#include<iostream>
#include<assert.h>

namespace wjh
{
	template <class T>
	struct _list_node
	{
		_list_node<T>* _prev = nullptr;
		_list_node<T>* _next = nullptr;
		T _data;

		_list_node(const T& val = T())
			:_data(val)
		{}
	};
	template <class T, class Ref, class Ptr>
	struct _list_iterator
	{
		typedef _list_node<T> node;
		typedef _list_iterator<T,Ref, Ptr> self;
		node* _node ;

		_list_iterator(node* ptr)
			:_node(ptr)
		{}

		_list_iterator(const self& v)
		{
			_node = v._node;
		}

		self& operator++()
		{
			_node = _node->_next;
			
			return *this;
		}

		self operator++(int)
		{
			self tmp(*this);
			_node = _node->_next;

			return tmp;
		}

		self& operator--()
		{
			_node = _node->_prev;

			return *this;
		}

		self& operator--(int)
		{
			self tmp(*this);
			_node = _node->_prev;

			return tmp;
		}

		Ref operator*()
		{
			return _node->_data;
		}

		bool operator!=(self& v)
		{
			return _node != v._node;
		}

		bool operator==(self& v)
		{
			return _node == v._node;
		}

		Ptr operator->()
		{
			return &_node->_data;
		}
	};
	template <class iterator, class Ref, class Ptr>
	class Reverse_iterator
	{
		typedef Reverse_iterator<iterator, Ref, Ptr> self;
	public:
		Reverse_iterator(iterator it)
			:_cur(it)
		{}

		self& operator++()
		{
			--_cur;
			return *this;
		}

		self operator++(int)
		{
			self tmp(*this);
			--_cur;
			return tmp;
		}

		self& operator--()
		{
			++_cur;
			return *this;
		}

		self operator--(int)
		{
			self tmp(*this);
			++_cur;
			return tmp;
		}

		Ref operator*()
		{
			iterator tmp = _cur;
			return *--tmp;
		}

		bool operator!=(self& it)
		{
			return _cur != it._cur;
		}

		Ptr operator->()
		{
			iterator tmp = _cur;
			return &(*--tmp);
		}

	private:
		iterator _cur;
	};

	template<class T>
	class list
	{
	public:
		typedef _list_node<T> node;
		typedef _list_iterator<T, T&, T*> iterator;
		typedef _list_iterator<T, const T&, const T*> const_iterator;
		typedef Reverse_iterator<iterator, T&, T*> reverse_iterator;
		typedef Reverse_iterator<const_iterator, const T&, const T*>const_reverse_iterator;
		void empty_init()
		{
			_head = new node;
			_head->_prev = _head;
			_head->_next = _head;
		}

		void swap(list<T>&tmp)
		{
			std::swap(_head, tmp._head);
		}

		list()
		{
			empty_init();
		}

		list(const list<T>&x)
		{
			empty_init();
			list tmp(x.begin(), x.end());
			swap(tmp);
		}

		template<class InputIterator>
		list(InputIterator first, InputIterator last)
		{
			empty_init();
			while (first != last)
			{
				push_back(*first);
				++first;
			}
		}

		list<T>& operator=(list<T>tmp)
		{
			swap(tmp);
			return *this;
		}

		~list()
		{
			clear();
			delete _head;
			_head = nullptr;
		}

		/*void clear()
		{
			iterator it = begin();
			node* cur = it._node;
			while (it != end())
			{
				cur = it._node;
				it++;
				delete cur;
			}
			it._node->_next = _head;
			it._node->_prev = _head;
		}*/

		void clear()
		{
			iterator it = begin();
			while (it != end())
			{
				erase(it++);
			}
		}

		void push_back(const T& val)
		{
			/*node* _new_node = new node;
			_new_node->_data = val;
			node* tail = _head->_prev;

			tail->_next = _new_node;
			_new_node->_prev = tail;
			_new_node->_next = _head;
			_head->_prev = _new_node;*/
			insert(end(),val);
		}

		void push_front(const T& val)
		{
			insert(begin(), val);
		}

		void pop_front()
		{
			erase(begin());
		}

		void pop_back()
		{
			erase(--end());
		}

		void insert(iterator pos, const T& val)
		{
			node* prev = pos._node->_prev;
			node* next = pos._node;

			node* new_node = new node(val);
			//node* new_node = new node;
			//new_node->_data = val;
			new_node->_prev = prev;
			prev->_next = new_node;
			new_node->_next = next;
			next->_prev = new_node;
		}

		iterator erase(iterator pos)
		{
			assert(pos._node != _head);
			node* prev = pos._node->_prev;
			node* next = pos._node->_next;
			node* current_node = pos._node;

			prev->_next = next;
			next->_prev = prev;
			delete current_node;

			return iterator(next);
		}

		iterator begin()
		{
			return iterator(_head->_next);
		}

		iterator end()
		{
			return iterator(_head);
		}

		const_iterator begin() const
		{
			return const_iterator(_head->_next);
		}

		const_iterator end() const
		{
			return const_iterator(_head);
		}

		reverse_iterator rbegin()
		{
			return reverse_iterator(end());
		}

		reverse_iterator rend()
		{
			return reverse_iterator(begin());
		}

	private:
		node* _head = nullptr;
	};

	void testlist1()
	{
		list<int> li;
		li.push_back(1);
		li.push_back(2);
		li.push_back(3);
		li.push_back(4);
		li.push_back(5);

		list<int>::iterator it = li.begin();
		while (it != li.end())
		{
			(*it) *= 2;
			std::cout << *it << " ";
			it++;
		}
		std::cout << std::endl;
	}

	void func(const list<int>& li)
	{
		list<int>::const_iterator it = li.begin();
		while (it != li.end())
		{
			//(*it) *= 2;
			std::cout << *it << " ";
			it++;
		}
		std::cout << std::endl;
	}

	void testlist2()
	{
		list<int> li;
		li.push_back(1);
		li.push_back(2);
		li.push_back(3);
		li.push_back(4);
		li.push_back(5);

		list<int>::iterator it = li.begin();
		while (it != li.end())
		{
			//(*it) *= 2;
			std::cout << *it << " ";
			it++;
		}
		std::cout << std::endl;
	}

	struct Test
	{
		int _a1;
		int _a2;

		Test(int a1 = 0, int a2 = 0)
			:_a1(a1)
			, _a2(a2)
		{}

		Test* const operator->()
		{
			return this;
		}
	};

	void testlist3()
	{
		list<int> li;
		li.push_back(1);
		li.push_back(2);
		li.push_back(3);
		li.push_back(4);
		li.push_back(5);
		li.insert(li.end(), 100);
		li.insert(li.begin(), 1000);
		wjh::list<int>::iterator pos = li.erase(li.begin());
		li.erase(pos);
		li.erase(--li.end());
		li.clear();

		list<int>::iterator it = li.begin();
		while (it != li.end())
		{
			//(*it) *= 2;
			std::cout << *it << " ";
			it++;
		}
		std::cout << std::endl;
	}

	void testlist4()
	{
		list<int> li;
		li.push_back(1);
		li.push_back(2);
		li.push_back(3);
		li.push_back(4);
		li.push_back(5);

		list<int> IT(li);
		list<int>::iterator it = IT.begin();
		while (it != IT.end())
		{
			//(*it) *= 2;
			std::cout << *it << " ";
			it++;
		}
		std::cout << std::endl;
	}

	void testlist5()
	{
		list<int> li;
		li.push_back(1);
		li.push_back(2);
		li.push_back(3);
		li.push_back(4);
		li.push_back(5);

		list<int> IT;
		IT = li;
		list<int>::iterator it = IT.begin();
		while (it != IT.end())
		{
			//(*it) *= 2;
			std::cout << *it << " ";
			it++;
		}
		std::cout << std::endl;

		list<int>::iterator It = li.begin();
		while (It != li.end())
		{
			//(*it) *= 2;
			std::cout << *It << " ";
			It++;
		}
		std::cout << std::endl;
	}

	void testlist6()
	{
		list<int> li;
		li.push_back(1);
		li.push_back(2);
		li.push_back(3);
		li.push_back(4);
		li.push_back(5);

		list<int>::iterator It = li.begin();
		while (It != li.end())
		{
			//(*it) *= 2;
			std::cout << *It << " ";
			++It;
		}
		std::cout << std::endl;

		list<int>::reverse_iterator it = li.rbegin();
		while (it != li.rend())
		{
			//(*it) *= 2;
			std::cout << *it << " ";
			++it;
		}
		std::cout << std::endl;
	}
}

